package com.blog.service.Impl;

import java.util.List;
import java.util.Random;

import com.blog.BO.PostBO;
import com.blog.VO.PostVO;
import com.blog.dao.AllPostDao;
import com.blog.dao.CheckPostDao;
import com.blog.dao.MidTableDao;
import com.blog.dao.MultilistDao;
import com.blog.dao.NoteDao;
import com.blog.dao.SayingDao;
import com.blog.dao.impl.AllPostDaoImpl;
import com.blog.dao.impl.CheckPostDaoImpl;
import com.blog.dao.impl.MidTableDaoImpl;
import com.blog.dao.impl.MultilistDaoImpl;
import com.blog.dao.impl.NoteDaoImpl;
import com.blog.dao.impl.SayingDaoImpl;
import com.blog.entity.Note;
import com.blog.entity.Page;
import com.blog.entity.Post;
import com.blog.entity.Saying;
import com.blog.service.PostService;

public class PostServiceImpl implements PostService {
	private AllPostDao apd = new AllPostDaoImpl();
	private NoteDao nd=new NoteDaoImpl();
	private MidTableDao mtd = new MidTableDaoImpl();
	private MultilistDao mld = new MultilistDaoImpl();
	private SayingDao sd=new SayingDaoImpl();
	private CheckPostDao cpd= new  CheckPostDaoImpl();
	@Override
	public int deletePostById(int postId) {
		// TODO Auto-generated method stub
		Post pt=apd.selectPostByPostId(postId);
		int num = cpd.insertCheckPost(pt);
		if(num ==1) {
			return apd.deletePostByPostId(postId);
		}
		return -1;
	}

	@Override
	public int insertById(Post post) {
		// TODO Auto-generated method stub
		int num1=cpd.insertCheckPost(post);
		PostBO pb= new PostBO();
		pb.setCreationTime(post.getCreationTime());
		List<Post> data=cpd.selectCheckPost(pb, 1, 1);
		Post pt=data.get(0);
		int num = pt.getPostState();
		if(num==1&& num1==1) {
			cpd.deleteCheckPostByPostId(pt.getPostId());
			return apd.insertPost(pt);
		}
		return -1;
	}
	@Override
	public int updatePostById(Post post) {
		// TODO Auto-generated method stub
		return apd.updatePost(post);
	}

	@Override
	public Page<Post> selectAllPost(int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		List<Post> data=apd.selectPost(null, pageNum, pageSize);
		int total =apd.selectPostNum();
		return new Page<Post>(data, pageNum, pageSize, total);
	}

	@Override
	public Post selectPostById(int postId) {
		// TODO Auto-generated method stub
		return apd.selectPostByPostId(postId);
	}

	@Override
	public int updatePostById(int PostUserId, int userId) {
		// TODO Auto-generated method stub
		return updatePostById(PostUserId, userId);
	}

	@Override
	public int selectPostStar(int postId) {
		// TODO Auto-generated method stub
		return mtd.selectPostStarNumByPostId(postId);
	}

	@Override
	public int selectPostLike(int postId) {
		// TODO Auto-generated method stub
		return mtd.selectPostlikeNumByPostId(postId);
	}

	@Override
	public Page<PostVO> selectPostByNum(int num) {
		// TODO Auto-generated method stub
		List<PostVO> data=mld.selectBestLikePostByNum(num);
		return new Page<PostVO>(data, 1, 1, 0);
	}

	@Override
	public Page<Post> selectAllPostById(int userId, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		List<Post> data=apd.selectAllPostByPostUserId(userId,pageNum,pageSize);
		int num=data.size();
		int total =apd.selectPostNum();
		return new Page<Post>(data, pageNum, pageSize, total);
	}

	@Override
	public Saying selectSaying() {
		// TODO Auto-generated method stub
		int max = sd.selectAllSayingNum();
		Random random = new Random();
		int s=random.nextInt(max)%(max-1 +1)+1;
		return sd.selectSayingByOrder(s);
	}

	@Override
	public Note selectNote() {
		// TODO Auto-generated method stub
		return nd.selectLastNote();
	}

	@Override
	public Page<Post> selectPost(Post post, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		PostBO pb= new PostBO();
		pb.setPostTitle(post.getPostTitle());
		List<Post> data=apd.selectPost(pb, pageNum, pageSize);
		int total =apd.selectPostNum();
		return new Page<Post>(data, pageNum, pageSize, total);
	}

	@Override
	public Page<Post> selectAllCheckPost(int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		List<Post> data=cpd.selectCheckPost(null, pageNum, pageSize);
		int total=cpd.selectCheckPostNum();
		return new Page<Post>(data, pageNum, pageSize, total);
	}

	@Override
	public int deletePost(int postId, int userId) {
		// TODO Auto-generated method stub
		Post post=apd.selectPostByPostId(postId);
		if(userId==post.getPostUserId()) {
			return apd.deletePostByPostId(postId);
		}
		return -1;
	}

	

}
